package com.luoxiang.socialandroid.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.bean.LableLib;
import com.luoxiang.socialandroid.bean.THost;

import java.util.List;

public class LableAdapter extends RecyclerView.Adapter<LableAdapter.ViewHolder> {
    public LableAdapter(List<LableLib> list) {
        this.list = list;
    }

    public void setData(List<LableLib> list) {
        this.list = list;
    }

    private List<LableLib> list;

    public static interface OnAddClickListener {
        void onItemClicks(int position, int id, boolean isselect, String name); //传递boolean类型数据给activity
    }

    LableAdapter.OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(LableAdapter.OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_lable, parent, false);
        LableAdapter.ViewHolder holder = new LableAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        LableLib lib = list.get(position);
        holder.tx_lable.setText(lib.getName());
        if (lib.isSelect()) {
            //选中
            holder.tx_lable.setTextColor(Color.parseColor("#835DFF"));
            holder.itemView.setBackgroundResource(R.drawable.shape_lable_c);
        } else {
            //不选中
            holder.tx_lable.setTextColor(Color.parseColor("#999999"));
            holder.itemView.setBackgroundResource(R.drawable.shape_lable);
        }
//        holder.itemView.setBackgroundColor(Color.parseColor(lib.getColor()))
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean selected = lib.isSelect();
                onItemAddClick.onItemClicks(position, list.get(position).getId(), !selected, lib.getName());
            }
        });

    }

    @Override
    public int getItemCount() {
        try {
            return list.size();
        } catch (Exception e) {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tx_lable;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tx_lable = itemView.findViewById(R.id.tx_lable);
        }
    }
}
